//
//  View2.m
//  An_Minh_Dat
//
//  Created by HOCVIEN on 4/21/15.
//  Copyright (c) 2015 phan hoang quoc an. All rights reserved.
//

#import "View2.h"
#import <QuartzCore/QuartzCore.h>

@interface View2 ()
@property (strong,nonatomic) NSDictionary *recipe;
@property (strong,nonatomic) NSString *basicString;


@end

@implementation View2

- (void)viewDidLoad {
    [self.ActivityIndicator_view2 startAnimating];
    self.ActivityIndicator_view2.alpha = 1;
    //border cho imageView va Button
    self.ImageViewBenTrong_view2.layer.borderWidth = 3.0f;
    self.ImageViewBenTrong_view2.layer.borderColor = [[UIColor whiteColor] CGColor];
    UIColor *mainColor = [self colorFromHexString:@"#F5A623"];
    self.pre_view2.layer.borderWidth = 2.0f;
    self.pre_view2.layer.borderColor = [[UIColor orangeColor] CGColor];
    
    self.Next_view2.layer.borderWidth = 2.0f;
    self.Next_view2.layer.borderColor = [[UIColor orangeColor] CGColor];
    
    
    [self.navigationController.navigationBar setBarTintColor:mainColor];
    [self.pre_view2 setBackgroundColor:mainColor];
    [self.Next_view2 setBackgroundColor:mainColor];
    self.TextVIew_view2.backgroundColor = [UIColor clearColor];
    self.TextVIew_view2.font = [UIFont fontWithName:@"American typewriter" size:13.0f];
    
    
    
    
    
    
    self.basicString = @"http://food2fork.com/api/get?key=11b98252974bfa489035a46f02333101&rId=";
    if (self.appArr_View2) {
        [self processLoad:self.indexRow_view2];
        
}
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (UIColor *)colorFromHexString:(NSString *)hexString {
    unsigned rgbValue = 0;
    NSScanner *scanner = [NSScanner scannerWithString:hexString];
    [scanner setScanLocation:1]; // bypass '#' character
    [scanner scanHexInt:&rgbValue];
    return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
}
- (IBAction)PrevAction:(id)sender {
    long count = self.appArr_View2.count;
    if (self.indexRow_view2 > 0) {
        self.indexRow_view2--;
        [self processLoad:self.indexRow_view2];
        
    }else {
        [self processLoad:(count-1)];
        
    }
    
    
}
- (IBAction)NextAction:(id)sender {
    long count = self.appArr_View2.count;
    if (self.indexRow_view2<count) {
        self.indexRow_view2 ++;
        [self processLoad:self.indexRow_view2];
        
    }else{
        [self processLoad:0];
        
    }
}
- (void)processLoad:(long)index{
    NSDictionary *dictionaryDetail = self.appArr_View2[index];
    
    NSString *Id = dictionaryDetail[@"recipe_id"];
    
    
    NSString *stringRecipes = [NSString stringWithFormat:@"%@%@",self.basicString,Id];
    
    
    NSURL *urlRecipe = [NSURL URLWithString:stringRecipes];
    NSURLRequest *requestRecipe = [NSURLRequest requestWithURL:urlRecipe];
    [self.ActivityIndicator_view2 startAnimating];
    self.ActivityIndicator_view2.alpha = 1;
    
    
    [NSURLConnection sendAsynchronousRequest:requestRecipe queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dicRecipe = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        self.recipe = dicRecipe[@"recipe"];
        
        NSArray *ArrIngredients = self.recipe[@"ingredients"];
        NSString * Incerdients = @"";
        for (int i = 0; i<ArrIngredients.count; i++) {
            NSString *Ingre = ArrIngredients[i];
            if (i==0) {
                Incerdients = [NSString stringWithFormat:@"%@%@",Incerdients,Ingre];
            }else{
                Incerdients = [NSString stringWithFormat:@"%@\n%@",Incerdients,Ingre];
                
            }
        }
        
        NSString *linkHinh = self.recipe[@"image_url"];
        NSURL *urlHinh = [NSURL URLWithString:linkHinh];
        dispatch_async(dispatch_queue_create("loadhinh", nil), ^{
            NSData *dataHinh = [NSData dataWithContentsOfURL:urlHinh];
            UIImage *image =[UIImage imageWithData:dataHinh];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.ActivityIndicator_view2 stopAnimating];
                self.ActivityIndicator_view2.alpha = 0;
                NSString *titleText = self.recipe[@"title"];
                UILabel* titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
                titleLabel.font = [UIFont fontWithName:@"American typewriter" size:20.0f];
                //titleLabel.textAlignment = UITextAlignmentCenter;
                titleLabel.text = titleText;
                
                
                self.navigationItem.titleView = titleLabel;
                self.TextVIew_view2.text = Incerdients;
                [self.ImageViewBenNgoai_view2 setImage:image];
                [self.ImageViewBenTrong_view2 setImage:image];
                
                
                
            });
        });
        
        
    }];

    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
